package com.lkd.http.controller;

import com.lkd.entity.RegionEntity;
import com.lkd.http.vo.RegionReq;
import com.lkd.service.RegionService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private RegionService regionService;
    /**
     * 分页搜索
     *
     * @param pageIndex 当前页
     * @param pageSize  每页展示的条数
     * @param name      模糊查询的名字
     * @return
     */
    /**
     * @RequestParam QueryString注解
     * value  需要映射的字段名字
     * required 前端不传参数的时候，会将参数置为null
     * defaultValue  默认值
     */
    @GetMapping("/search")
    public Pager<RegionEntity> getRegionByPage(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Long pageIndex,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "1") Long pageSize,
                                               @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        return regionService.getRegionByPage(pageIndex, pageSize, name);

    }


    /**
     * 添加区域
     *
     * @param regionReq 前端请求参数的封装
     * @return Boolean
     */
    @PostMapping
    public Boolean addRegion(@RequestBody RegionReq regionReq) {
        //将前端请求参数转换成实体类
        RegionEntity regionEntity = new RegionEntity();
        regionEntity.setName(regionReq.getRegionName());
        regionEntity.setRemark(regionReq.getRemark());
        return regionService.addRegion(regionEntity);
    }


    /**
     * 更新区域
     *
     * @param id        区域的id值
     * @param regionReq 前端数据的封装
     * @return boolean
     */
    @PutMapping("/{id}")
    public Boolean updateRegion(@PathVariable("id") Long id,
                                @RequestBody RegionReq regionReq) {
        //将前端数据转换成有效实体类传入
        RegionEntity regionEntity = new RegionEntity();
        regionEntity.setId(id);
        regionEntity.setName(regionReq.getRegionName());
        regionEntity.setRemark(regionReq.getRemark());
        return regionService.updateRegion(id, regionEntity);
    }

    /**
     * 删除区域
     * @param regionId  删除区域信息的id值
     * @return boolean
     */
    @DeleteMapping("/{id}")
    public Boolean deleteRegion(@PathVariable("id")String regionId){
        return regionService.deleteRegion(regionId);
    }





}
